home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 11100 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.3 KB

  1. Path: erich.triumf.ca!bennett
  2. From: bennett@erich.triumf.ca (P.Bennett)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Problem with scanf
  5. Date: 21 Mar 1996 15:12 PST
  6. Organization: TRIUMF: Tri-University Meson Facility
  7. Distribution: world
  8. Message-ID: <21MAR199615124123@erich.triumf.ca>
  9. References: <4iscgb$8h9@newsflash.concordia.ca>
  10. NNTP-Posting-Host: ftp.triumf.ca
  11. News-Software: VAX/VMS VNEWS 1.50    
  12.  
  13. In article <4iscgb$8h9@newsflash.concordia.ca>, v_ruso@alcor.concordia.ca writes...
  14. >I am writing a program that is supposed to gather information from the
  15. >user.
  16. >The program works fine if the user enters some numbers and presses
  17. >enter.  But if the user just presses enter without entering any data,
  18. >the cursor just starts a new line.  
  19. >Can anyone tell me how I can avoid this?
  20. >this is the part of the code i am refering to:
  21. >..
  22. >..
  23. >gotoxy (13,22);
  24. >    cprintf ("The maximum load the cylinder is expected to handle");
  25. >    gotoxy (15,10);
  26. >    cprintf ("Enter the Maximum Axial Load [Lbf]: ");
  27. >    xlocation = wherex();
  28. >    gotoxy (xlocation,10);
  29. >    scanf ("%f",&maxload);
  30.  
  31. scanf() is very nasty for direct user input if the user enters "unexpected"
  32. things.  The usual recommendation is to get the user's input with fgets(), then
  33. parse it with sscanf(), or other things, as needed.  You can then easily check
  34. for valid input, and recover from errors.
  35.  
  36. However, since you are using the DOS-specific (and non-portable) cprintf() 
  37. and gotoxy(), you should use a corresponding input function - namely cgets(). 
  38. Read the description on cgets() _very_ carefully - it is a bit strange!! 
  39. Before you call it, you set the first char in its buffer to the maximun number
  40. of chars to get, then it sets the second char to the number it got, and the
  41. string actually starts in the third position (buffer[2]).  
  42.  
  43. This all applies _only_ to Borland compilers - Microsoft may have a different
  44. input function (I know they call "gotoxy()" something else...)
  45.  
  46.  
  47. Peter Bennett VE7CEI                | Vessels shall be deemed to be in sight
  48. Internet: bennett@triumf.ca         | of one another only when one can be
  49. Packet: ve7cei@ve7kit.#vanc.bc.ca   | observed visually from the other
  50. TRIUMF, Vancouver, B.C., Canada     |                          ColRegs 3(k)
  51. GPS and NMEA info and programs: ftp://sundae.triumf.ca/pub/peter/index.html
  52. or: ftp://ftp-i2.informatik.rwth-aachen.de/pub/arnd/GPS/peter/index.html
  53.  
  54.